package com.buer.train.business.service.impl;

import com.buer.train.business.dao.MenuDao;
import com.buer.train.business.dao.RoleMenuDao;
import com.buer.train.business.domain.MenuDO;
import com.buer.train.business.domain.RoleMenuDO;
import com.buer.train.business.service.MenuService;
import com.buer.train.common.utils.BuildTree;
import com.buer.train.common.utils.Tree;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDao menuDao;

	@Autowired
	private RoleMenuDao roleMenuDao;
	
	@Override
	public MenuDO get(Integer menuId){
		return menuDao.get(menuId);
	}
	
	@Override
	public List<MenuDO> list(Map<String, Object> map){
		return menuDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return menuDao.count(map);
	}
	
	@Override
	public int save(MenuDO menu){
		return menuDao.save(menu);
	}
	
	@Override
	public int update(MenuDO menu){
		return menuDao.update(menu);
	}
	
	@Override
	public int remove(Integer menuId){
		return menuDao.remove(menuId);
	}

	@Override
	@Transactional
	public int removeLoop(Integer menuId) {
		//递归删除
		menuDao.remove(menuId);
		doRemoveLoop(menuId);
		return 1;
	}

	private void doRemoveLoop(Integer menuId){
		Map map=new HashMap();
		map.put("parentId",menuId);
		List<MenuDO> list = menuDao.list(map);
		for (MenuDO menuDO:list){
			menuDao.remove(menuDO.getMenuId());
			doRemoveLoop(menuDO.getMenuId());
		}
	}

	@Override
	public int batchRemove(Integer[] menuIds){
		return menuDao.batchRemove(menuIds);
	}

    @Override
    public PageInfo<MenuDO> getMenuList(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<MenuDO> list = menuDao.getMenuList(map); // 获取数据
        PageInfo<MenuDO> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public MenuDO getOne(Map<String, Object> map){
        return menuDao.getOne(map);
    }

	@Override
	public List<MenuDO> getAllResourceByUserId(Integer userId) {
		return menuDao.getAllResourceByUserId(userId);
	}

	@Override
	public Tree<MenuDO> getTree(Map<String, Object> params) {
		List<Tree<MenuDO>> trees = new ArrayList<>();
		List<MenuDO> arrayList =new ArrayList<>();
		if(params.size()>0){
			// 如果传入了条件角色id
			if(params.get("roleId")!=null){
				Map<String,Object> queryMap=new HashMap<>(2);
				queryMap.put("roleId",params.get("roleId"));
				List<RoleMenuDO> list = roleMenuDao.list(queryMap);
				List<MenuDO> list1 = menuDao.list(null);
				List<Integer> temp=new ArrayList<>();
				for (RoleMenuDO roleMenuDO:list){
					temp.add(roleMenuDO.getMenuId());
				}
				List<Integer> menuIds=temp;
				/*for(MenuDO menuDO:list1){
					if (temp.contains(menuDO.getParentId())) {
						menuIds.remove(menuDO.getParentId());
					}
				}*/
				for (MenuDO menuDO:list1) {
					Tree<MenuDO> tree = new Tree<MenuDO>();
					tree.setId(menuDO.getMenuId()+"");
					tree.setParentId(menuDO.getParentId()+"");
					tree.setText(menuDO.getName());
					Map<String, Object> state = new HashMap<>(16);
					Integer id = menuDO.getMenuId();
					if (menuIds.contains(id)) {
						state.put("selected", true);
					} else {
						state.put("selected", false);
					}
					tree.setState(state);
					trees.add(tree);
				}
			}
		}else {
			arrayList = menuDao.list(null);
			for (MenuDO menuDO : arrayList) {
				Tree<MenuDO> tree = new Tree<>();
				tree.setId(menuDO.getMenuId()+"");
				tree.setParentId(menuDO.getParentId()+"");
				tree.setText(menuDO.getName());
				trees.add(tree);
			}
		}
		// 默认顶级菜单为０或空，根据数据库实际情况调整
		return BuildTree.build(trees);
	}

}
